<?php

class ThanhNien {

    var $links = array();
    var $title = "";
    var $intro = "";
    var $content = "";
    var $imgurl = "";

    function Init() {
        $this->title = "";
        $this->intro = "";
        $this->content = "";
        $this->imageurl = "";
    }

    function GetLink($link) {
        $link = trim($link);
        $document = TextBetween('<div class="left-news-content">', '<div class="right-news-content">', FetchDataString($link));

        preg_match_all('/\bhttp:\/\/www.thanhnien.com.vn\/news\/Pages[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|]/i', $document, $result, PREG_PATTERN_ORDER);

        $links = array();

        $n = sizeof($result[0]);

        for ($i = 0; $i < $n; $i++) {
            $flag = 0;
            for ($j = $i + 1; $j < $n; $j++)
                if ($result[0][$j] == $result[0][$i])
                    $flag = 1;

            if ($flag == 0)
                array_push($this->links, $result[0][$i]);
        }

        return true;
    }

    function GetInfo($link) {
        $this->Init();

        $document = Unicode_Decode(FetchDataString($link));

        $search = array('%<img.*?src=[\'"]?(/.*(?:jpg|gif|png|bmp))[\'"]?.*?>%i');
        $replace = array('<img src="http://www.thanhnien.com.vn\1">');

        $document = preg_replace($search, $replace, $document);

        $pos1 = strpos($document, '<!-- Main Title  -->');
        $pos2 = strrpos($document, '<!-- Page Img & Content  -->');

        $document = trim(substr($document, $pos1, $pos2 - $pos1));

        //echo $document;

        $this->title = TextBetween('<div class="mainTitle">', '</div>', $document);

        if (!preg_match('/<img.*?src="(.*(?:jpg|gif|png|bmp))".*?>/i', $document, $regs))
            $this->imgurl = "";
        else
            $this->imgurl = $regs[1];

        $this->intro = TextBetween('<div class="pageDes">', '</div>', $document);

        $pos1 = strpos($document, '<div class="pageContent">');
        $pos2 = strrpos($document, '</p>');

        $this->content = trim(substr($document, $pos1));

        return true;
    }

}

?>